<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Exceptions\InvalidRequestException;
use App\Models\Labor;
use App\Models\ResourceBind;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaborService
{
    public function get($title)
    {
        $sql = Labor::when($title, function ($query, $title) {
            return $query->where(function ($query) use ($title) {
                $query->where('name', 'like', '%' . $title . '%');
            });
        });
        return $sql;
    }

    public function insert($input)
    {
        //判断是否存在
        $is = Labor::where('mobile', $input['mobile'])->where('company_id', $input['company_id'])->first();
        if ($is) {
            throw new InvalidRequestException('该手机号码已存在');
        }

        if ($input['birth']) {
            $input['birth'] = Date("Y-m-d", $input['birth'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if ($v) {
                $res[$k] = $v;
            }
        }

        $files = [];
        if(!empty($res['files'])){
            $files = explode(',',$res['files']);
        }
        if(!empty($res['third_edu'])){
            $third_edu = explode(',',$res['third_edu']);
            $files = array_merge($files,$third_edu);
        }
        if(!empty($res['paper_file'])){
            $paper_file = explode(',',$res['paper_file']);
            $files = array_merge($files,$paper_file);
        }
        if(!empty($res['id_card_file'])){
            $id_card_file = explode(',',$res['id_card_file']);
            $files = array_merge($files,$id_card_file);
        }

        try {
            $data = DB::transaction(function () use ($res,$files) {
                $data = Labor::create($res);

                $bind_insert = [];
                foreach ($files as $k => $v) {
                    $bind = [];
                    $bind['source_id'] = $data->id;
                    $bind['resource_id'] = $v;
                    $bind['model'] = 'Labor';
                    $bind_insert[] = $bind;
                }
                ResourceBind::insert($bind_insert);
                //添加到人员库存
                if($data){
                    return $data->id;
                }
            });

            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function insertBatch($input)
    {
        $is = Labor::where('company_id', $input['company_id'])->get();
        $mobiles = [];
        $id_cards = [];
        foreach ($is as $k => $v) {
            $mobiles[] = $v->mobile;
            $id_cards[] = $v->id_card;
        }
        $labors = json_decode($input['labors']);
        $insert = [];
        foreach ($labors as $k => $v) {
            if($k > 0){
                if(in_array($v[1],$mobiles)){
                    throw new InvalidRequestException('第'.$k.'行该手机号码已存在');
                }
                if(in_array($v[2],$id_cards)){
                    throw new InvalidRequestException('第'.$k.'行该身份证号码已存在');
                }
                $arr = [];
                $arr['name'] = $v[0];
                $arr['mobile'] = $v[1];
                $arr['id_card'] = $v[2];
                $arr['bank_deposit'] = $v[3];
                $arr['bank_number'] = $v[4];
                $arr['bank'] = $v[5];
                $arr['work'] = $v[6];
                $arr['admin_id'] = $input['admin_id'];
                $arr['company_id'] = $input['company_id'];
                $insert[] = $arr;
            }
        }
        try {
            $data = Labor::insert($insert);
            //添加到人员库存
            if($data){
                return $data;
            }
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }


    public function update($input)
    {
        //判断是否存在
        $is = Labor::where('mobile', $input['mobile'])->where('company_id', $input['company_id'])->first();
        if ($is && $is->id != $input['id']) {
            throw new InvalidRequestException('该手机号码绑定其他人员');
        }

        if ($input['birth']) {
            $input['birth'] = Date("Y-m-d", $input['birth'] / 1000);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if ($v) {
                $res[$k] = $v;
            }
        }
        unset($res['id']);

        $files = [];
        if(!empty($res['files'])){
            $files = explode(',',$res['files']);
        }
        if(!empty($res['third_edu'])){
            $third_edu = explode(',',$res['third_edu']);
            $files = array_merge($files,$third_edu);
        }
        if(!empty($res['paper_file'])){
            $paper_file = explode(',',$res['paper_file']);
            $files = array_merge($files,$paper_file);
        }
        if(!empty($res['id_card_file'])){
            $id_card_file = explode(',',$res['id_card_file']);
            $files = array_merge($files,$id_card_file);
        }

        try {

            $data = DB::transaction(function () use ($input,$res,$files) {
                $data = Labor::where('id', $input['id'])->update($res);
                $bind_insert = [];
                foreach ($files as $k => $v) {
                    $bind = [];
                    $bind['source_id'] = $input['id'];
                    $bind['resource_id'] = $v;
                    $bind['model'] = 'Labor';
                    $bind_insert[] = $bind;
                }
                ResourceBind::where('source_id',$input['id'])->where('model','Labor')->delete();
                if(count($bind_insert)){
                    ResourceBind::insert($bind_insert);
                }

                if($data){
                    return $input['id'];
                }
            });

            return $data;

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
}
